<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Controller;
use think\Request;
use app\common\model\Category as CateModel;
use app\common\model\Article;
class Category extends AdminBase
{
    private static $CateModel=null;
    private static $ArticleModel=null;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (self::$CateModel==null){
            self::$CateModel=new CateModel();
        }
        if (self::$ArticleModel==null){
            self::$ArticleModel=new Article();
        }
    }

    /**
     * 分类显示
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function lists(){
        $get = $_GET;
        unset($get['/admin/category/lists']);
        $where=[];
        if (isset($get['cate_name'])&&!empty($get['cate_name'])){
            $where['cate_name']=['like','%'.trim($get['cate_name']).'%'];
        }
        $CateData = self::$CateModel->where($where)->order('update_time desc')->paginate(15);
        $this->assign('CateData',$CateData);
        return $this->fetch();
    }

    /**
     * 分类添加
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){
        if (\request()->isGet()){
            return $this->fetch();
        }
        if (\request()->isAjax()){
            $post= input('post.');
            $validate = validate('Category');
            if (!$validate->check($post)){
                return $this->resMessage(0,$validate->getError());
            }
            $CateData = self::$CateModel->where('cate_name',$post['cate_name'])->find();
            if ($CateData!=null){
                return $this->resMessage(0,'分类已经存在');
            }
            $data= [
                'cate_name'=>$post['cate_name'],
                'create_time'=>time(),
                'update_time'=>time(),
            ];
            if (isset($post['status'])&&$post['status']=='on'){
                $data['status']=1;
            }else{
                $data['status']=0;
            }
            $res = self::$CateModel->save($data);
            if ($res){
                return $this->resMessage(1,'添加成功');
            }else{
                return $this->resMessage(0,'添加失败');
            }

        }

    }

    /**
     * 分类编辑操作
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
        if (\request()->isGet()){
            if (input('?param.id')){
                $id =input('param.id');
                $CateData = self::$CateModel->where('cate_id',$id)->find();
                $this->assign('CateData',$CateData);
                return $this->fetch();
            }else{
                abort(404);
            }

        }
        if (\request()->isAjax()){
            $post = input('post.');
            $validate = validate('Category');
            if (!$validate->check($post)){
                return $this->resMessage(0,$validate->getError());
            }
            if (!isset($post['id'])&&empty($post['id'])){
                return $this->resMessage(0,'缺少参数');
            }else{
                $id = $post['id'];
            }
            $data= [
                'cate_name'=>$post['cate_name'],
                'update_time'=>time(),
            ];
            if (isset($post['status'])&&$post['status']=='on'){
                $data['status']=1;
            }else{
                $data['status']=0;
            }
            $res = self::$CateModel->save($data,['cate_id'=>$id]);
            if ($res){
                return $this->resMessage(1,'操作成功');
            }else{
                return $this->resMessage(0,'操作失败');
            }


        }

    }

    /**
     * 分类删除
     * @return array
     * @throws \think\exception\DbException
     */
    public function del(){
        if (\request()->isAjax()){
            if (input('?post.id')){
                $id = input('post.id');
                $oldData = self::$CateModel->get($id);
                if ($oldData==null){
                    return $this->resMessage(0,'数据不存在');
                }
                $articleData =self::$ArticleModel->where('cate_id',$id)->find();
                if ($articleData!=null){
                    return $this->resMessage(0,'当前分类下有文章，不能删除，请先将文章删除');
                }
                $res = self::$CateModel->where('cate_id',$id)->delete();
                if ($res){
                    return $this->resMessage(1,'删除成功');
                }else{
                    return $this->resMessage(0,'删除失败');
                }
            }else{
                return $this->resMessage(0,'缺少参数');
            }
        }
    }

    /**
     * 更改分类状态
     * @return array
     * @throws \think\exception\DbException
     */
    public function upCateSta(){

        if (\request()->isAjax()){
            if (input('?post.id')&&input('?post.sta')){
                $id = input('post.id');
                $sta = input('post.sta');
                $oldData=  self::$CateModel->get($id);
                if ($oldData==null){
                    return $this->resMessage(0,'数据不存在');
                }
                $articleData =self::$ArticleModel->where('cate_id',$id)->find();
                if ($articleData!=null){
                    return $this->resMessage(0,'当前分类下有文章，不能停用，请先将文章删除');
                }
                $res = self::$CateModel->save(['status'=>!$sta],['cate_id'=>$id]);
                if ($res){
                    return $this->resMessage(1,'操作成功');
                }else{
                    return $this->resMessage(0,'操作失败');
                }
            }else{
                return $this->resMessage(0,'缺少参数');
            }
        }else{
            abort(404);
        }
    }
}
